package com.buka.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buka.domain.ResponseResult;
import com.buka.dto.RoleDto;
import com.buka.mapper.RoleMapper;
import com.buka.pojo.Role;
import com.buka.pojo.RoleMenu;
import com.buka.service.RoleMenuService;
import com.buka.service.RoleService;
import com.buka.util.BeanCopyUtils;
import com.buka.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
 * @createDate 2024-05-28 16:35:11
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if (id == 1L) {
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult list(int pageNum, int pageSize, String roleName, int status) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(roleName)) {
            queryWrapper.like(Role::getRoleName, roleName);
        }
        if (status > -1) {
            queryWrapper.eq(Role::getStatus, status);
        }
        Page<Role> page = new Page<>(pageNum, pageSize);
        Page<Role> rolePage = page(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", rolePage.getRecords());
        map.put("total", rolePage.getTotal());
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult changeStatus(RoleDto roleDto) {
        Role role = new Role();
        role.setStatus(role.getStatus());
        role.setId(role.getId());
        updateById(role);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult add(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        save(role);
        List<RoleMenu> roleMenuList = new ArrayList<>();
        for (Integer menuId : roleDto.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(new Long(menuId));
            roleMenuList.add(roleMenu);
        }
        roleMenuService.saveBatch(roleMenuList);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRoleById(int id) {
        Role byId = getById(id);
        RoleVo roleVo = BeanCopyUtils.copyBean(byId, RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @Override
    public ResponseResult updateRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        updateById(role);
        return null;
    }

    @Override
    public ResponseResult listAllRole() {
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.eq(Role::getStatus, 0);
        List<Role> roleList = list(roleLambdaQueryWrapper);
        return ResponseResult.okResult(roleList);
    }

    @Override
    public ResponseResult deleteRoles(Long[] ids) {
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.in(Role::getId, ids);
        getBaseMapper().delete(roleLambdaQueryWrapper);
        return ResponseResult.okResult();
    }

}




